/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.configuration;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLFilterImpl;

/**
 * 
 * @author noctarius
 * @since 1.9.7
 */
public class ForceNamespaceContentHandler extends XMLFilterImpl {
	private String usedNamespaceUri;
	private boolean addNamespace;

	// State variable
	private boolean addedNamespace = false;

	public ForceNamespaceContentHandler(final String namespaceUri,
			final boolean addNamespace) {
		if (addNamespace) {
			this.usedNamespaceUri = namespaceUri;
		} else {
			this.usedNamespaceUri = "";
		}
		this.addNamespace = addNamespace;
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();

		if (addNamespace) {
			startControlledPrefixMapping();
		}
	}

	@Override
	public void startElement(final String arg0, final String arg1,
			final String arg2, final Attributes arg3) throws SAXException {

		super.startElement(this.usedNamespaceUri, arg1, arg2, arg3);
	}

	@Override
	public void endElement(final String arg0, final String arg1,
			final String arg2) throws SAXException {

		super.endElement(this.usedNamespaceUri, arg1, arg2);
	}

	@Override
	public void startPrefixMapping(final String prefix, final String url)
			throws SAXException {

		if (addNamespace) {
			this.startControlledPrefixMapping();
		} else {
			// Remove the namespace, i.e. don´t call startPrefixMapping for
			// parent!
		}

	}

	private void startControlledPrefixMapping() throws SAXException {
		if (this.addNamespace && !this.addedNamespace) {
			// We should add namespace since it is set and has not yet been
			// done.
			super.startPrefixMapping("", this.usedNamespaceUri);

			// Make sure we dont do it twice
			this.addedNamespace = true;
		}
	}

}
